import XLSX from 'xlsx-style'

function saveAs(obj, fileName) {
	var tmpa = document.createElement("a");
	tmpa.download = fileName || "下载";
	tmpa.href = URL.createObjectURL(obj);
	tmpa.click();
	setTimeout(function() {
		URL.revokeObjectURL(obj);
	}, 100);
}

function getCharCol(n) {
	let s = '',
		m = 0
	while (n > 0) {
		m = n % 26 + 1
		s = String.fromCharCode(m + 64) + s
		n = (n - m) / 26
	}
	return s
}

function s2ab(s) {
	if (typeof ArrayBuffer !== 'undefined') {
		let buf = new ArrayBuffer(s.length);
		let view = new Uint8Array(buf);
		for (let i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
		return buf;
	} else {
		let buf = new Array(s.length);
		for (let i = 0; i != s.length; ++i) buf[i] = s.charCodeAt(i) & 0xFF;
		return buf;
	}
}
var wopts = {
		bookType: 'xlsx',
		bookSST: true,
		type: 'binary',
		cellStyles: true
	};
var type = "xlsx";
export const downloadExl = (params) => {
	let title = {};
	let keyMap = []; //获取keys
	let cols = [];
	let exportColumn =params['exportColumn'];
	let data =[];
	params['data'].forEach((e)=>{
		data.push(e);
	});
	let fileName =params['fileName'];
	for (let k in exportColumn) {
		keyMap.push(exportColumn[k].key);
		title[exportColumn[k].key] = exportColumn[k].title;
		cols.push({
			wpx: exportColumn[k].width ? exportColumn[k].width:100
		});
	}
	data.unshift(title);
	let tmpdata = []; //用来保存转换好的json

	data.map((v, i) => keyMap.map((k, j) => Object.assign({}, {
		v: v[k]?v[k]:'',
		position: (j > 25 ? getCharCol(j) : String.fromCharCode(65 + j)) + (i + 1)
	}))).reduce((prev, next) => prev.concat(next)).forEach((v, i) => tmpdata[v.position] = {
		v: v.v
	});
	let titleIndexArray = [];
	keyMap.forEach((k, j) => {
			titleIndexArray.push((j > 25 ? getCharCol(j) : String.fromCharCode(65 + j)))
		}

	)

	let outputPos = Object.keys(tmpdata); //设置区域,比如表格从A1到D10
	//        边框样式
	let border = {
		bottom: {
			style: "thin",
			color: {
				rgb: "000000"
			}
		},
		top: {
			style: "thin",
			color: {
				rgb: "000000"
			}
		},
		left: {
			style: "thin",
			color: {
				rgb: "000000"
			}
		},
		right: {
			style: "thin",
			color: {
				rgb: "000000"
			}
		}
	};
	//加粗
	//不加粗
	let styleNormal = {
		border: border,
		alignment: {
			horizontal: 'center',
			wrapText: true,
			vertical: "center"
		},
		font: {
			sz: 12,
			color: {
				rgb: "000000"
			},
			outline: true
		}
	};
	//蓝底加粗
	let styleTitle = {
		border: border,
		alignment: {
			horizontal: 'center',
			wrapText: true,
			vertical: "center"
		},
		font: {
			sz: 12,
			bold: true,
			color: {
				rgb: "000000"
			},
			outline: true
		},
		fill: {
			fgColor: {
				rgb: "00BFFF"
			}
		}
	};
	for (let i = 0; i < data.length; i++) {
		let style = i == 0 ? styleTitle : styleNormal;
		for (let j = 0; j < titleIndexArray.length; j++) {
			tmpdata[titleIndexArray[j] + (i + 1)].s = style;
		}
	}
	// let cols = [];
	// for (let i = 0; i < titleIndexArray.length; i++) {
	// 	cols.push({
	// 		wpx: 100
	// 	});
	// }
	tmpdata["!cols"] = cols;
	tmpdata["!rows"] = [{
		hpx:100
	}];
	let tmpWB = {
		SheetNames: ['sheet1'], //保存的表标题
		Sheets: {
			'sheet1': Object.assign({},
				tmpdata, //内容
				{
					'!ref': outputPos[0] + ':' + outputPos[outputPos.length - 1] //设置填充区域
				})
		}
	};
	let tmpDown = new Blob([s2ab(XLSX.write(tmpWB, {
			bookType: (type == undefined ? 'xlsx' : type),
			bookSST: false,
			type: 'binary'
		} //这里的数据是用来定义导出的格式类型
	))], {
		type: ""
	});
	saveAs(tmpDown, fileName + '.' + (wopts.bookType == "biff2" ? "xls" : wopts.bookType))
}

export default {
	downloadExl
}
